package wn.tcpip.mq.consumer.config;

import com.alibaba.fastjson.JSONObject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import wn.tcpip.socket.util.data.DateStyle;
import wn.tcpip.socket.util.data.DateUtil;

import javax.annotation.Resource;
import java.util.Map;

@Component("consumerListener")
public class ConsumerListener{
	Logger log = LogManager.getLogger(getClass());
	@Resource
	private JdbcTemplate jdbcTemplate;
	public void handleMessage(String message) {
	String sql ="INSERT INTO wn_lnglat_sb(imei,sjny,sj,state,longitude,latitude,height," +
			"speed,orentation,visiblesatellite,scoutsatellite,depth,expand,point_type,business_id,business_type)" +
			" VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		JSONObject jsonObject = JSONObject.parseObject(message);
		Object[] params = {jsonObject.getString("imei"),
				DateUtil.StringToString(jsonObject.getString("sjny"), DateStyle.YYYYMM),jsonObject.getString("sj"),
				jsonObject.getString("state"),jsonObject.getString("longitude"),
						jsonObject.getString("latitude"),
				jsonObject.getString("height"),
						jsonObject.getString("speed"),
						jsonObject.getString("orentation"),
						jsonObject.getString("visiblesatellite"),
						jsonObject.getString("scoutsatellite"),
						jsonObject.getString("depth"),
						jsonObject.getString("expand"),
						"WGS84",
						"",
					"gto6"};
		log.info("sb {}gps : {}", jsonObject.getString("imei"),message);
		jdbcTemplate.update(sql,params);
		Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap("select count(imei) c from  wn_lnglat_sb_new where imei=?", jsonObject.getString("imei"));
		 if(Integer.parseInt(stringObjectMap.get("c").toString())==0){
			 String sql2 ="INSERT INTO wn_lnglat_sb_new(imei,sjny,sj,state,longitude,latitude,height," +
					 "speed,orentation,visiblesatellite,scoutsatellite,depth,expand,point_type,business_id,business_type)" +
					 " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			 jdbcTemplate.update(sql2,params);
		 }else{
			 Object[] params2 = {					 DateUtil.StringToString(jsonObject.getString("sjny"), DateStyle.YYYYMM),jsonObject.getString("sj"),
					 jsonObject.getString("state"),jsonObject.getString("longitude"),
					 jsonObject.getString("latitude"),
					 jsonObject.getString("height"),
					 jsonObject.getString("speed"),
					 jsonObject.getString("orentation"),
					 jsonObject.getString("visiblesatellite"),
					 jsonObject.getString("scoutsatellite"),
					 jsonObject.getString("depth"),
					 jsonObject.getString("expand"),
					 "WGS84",
					 "",
					 "gto6",jsonObject.getString("imei")};
			 String sql2 ="update wn_lnglat_sb_new set sjny=?,sj=?,state=?,longitude=?,latitude=?,height=?," +
					 "speed=?,orentation=?,visiblesatellite=?,scoutsatellite=?,depth=?,expand=?,point_type=?,business_id=?,business_type=? where imei=?";
			 jdbcTemplate.update(sql2,params2);
		 }

	}
}